package edu.fyvtc.javaweb.demo.aspect;

import edu.fyvtc.javaweb.demo.common.DemoResult;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
@Slf4j
@Order(0)
public class ReferenceAspect {

    //本切面中没有定义Pointcut而是引用了LogAspect类中定义的Pointcut：
    //edu.fyvtc.javaweb.demo.aspect.LogAspect.insertPointcut()
    @SneakyThrows
    @Around("edu.fyvtc.javaweb.demo.aspect.LogAspect.insertPointcut()")
    public DemoResult sfsf(ProceedingJoinPoint proceedingJoinPoint) {
        log.info("Reference环绕增强----开始");
        DemoResult result = (DemoResult) proceedingJoinPoint.proceed();
        log.info("Reference环绕增强----结束");
        return result;
    }


    @Around("within(edu.fyvtc.javaweb.demo.controller.*)" +
            " && !edu.fyvtc.javaweb.demo.aspect.LogAspect.homePointcut()" +
            " && !edu.fyvtc.javaweb.demo.aspect.LogAspect.publishPointcut()")
    public Object hhh(ProceedingJoinPoint proceedingJoinPoint) {
        Object[] args = proceedingJoinPoint.getArgs();
        String methodName = proceedingJoinPoint.getSignature().getName();
        log.info("*****************************************************************************");
        log.info("AspectController {} args: {}", methodName, Arrays.toString(args));
        Object result = null;
        try {
            result = proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            log.error("报异常啦！");
            log.debug(Arrays.toString(e.getStackTrace()));
        }
        log.info("AspectController {} return: {}", methodName, result);
        log.info("""
                *****************************************************************************\s
                
                
                """);
        return result;
    }

}
